package com.csun.cmny.provider.service.impl;

import com.csun.cmny.base.enums.ErrorCodeEnum;
import com.csun.cmny.core.support.BaseService;
import com.csun.cmny.provider.dao.OmcOrderServiceItemMapper;
import com.csun.cmny.provider.dto.OrderServiceItemContentDto;
import com.csun.cmny.provider.dto.OrderServiceItemDto;
import com.csun.cmny.provider.exceptions.OmcBizException;
import com.csun.cmny.provider.model.domain.OmcOrderServiceItem;
import com.csun.cmny.provider.model.domain.OmcOrderServiceItemContent;
import com.csun.cmny.provider.model.domain.OmcServiceItem;
import com.csun.cmny.provider.service.OmcOrderService;
import com.csun.cmny.provider.service.OmcOrderServiceItemContentService;
import com.csun.cmny.provider.service.OmcOrderServiceItemService;
import com.csun.cmny.provider.service.OmcServiceItemService;
import com.csun.cmny.provider.vo.OrderServiceItemVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;
import com.csun.cmny.provider.model.domain.OmcOrder;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
@Transactional
public class OmcOrderServiceItemServiceImpl extends BaseService<OmcOrderServiceItem> implements OmcOrderServiceItemService {

    @Resource
    private OmcOrderServiceItemMapper omcOrderServiceItemMapper;

    @Resource
    private OmcOrderServiceItemContentService omcOrderServiceItemContentService;

    @Resource
    private OmcOrderService omcOrderService;

    @Resource
    private OmcServiceItemService omcServiceItemService;

    /*
    * 插入
    * */
    @Override
    public Integer saveOrderServiceItem(OrderServiceItemDto orderServiceItemDto) {

        //校验订单id是否存在
        checkOrderExist(orderServiceItemDto.getOrderId());

        //校验同一个服务项是否已经添加过
        checkHasSave(orderServiceItemDto);

        //插入订单服务项
        OmcOrderServiceItem omcOrderServiceItem = new OmcOrderServiceItem();
        BeanUtils.copyProperties(orderServiceItemDto, omcOrderServiceItem);
        int addOrderServiceItemRet = omcOrderServiceItemMapper.insertSelective(omcOrderServiceItem);
        //批量插入订单服务内容
        List<OrderServiceItemContentDto> orderServiceItemContentDtos = orderServiceItemDto.getOrderServiceItemContentDtos();
        List<OmcOrderServiceItemContent> omcOrderServiceItemContents = new ArrayList<>();
        if(orderServiceItemContentDtos.size() > 0){
            for(OrderServiceItemContentDto item : orderServiceItemContentDtos){
                OmcOrderServiceItemContent omcOrderServiceItemContent = new OmcOrderServiceItemContent();
                BeanUtils.copyProperties(item, omcOrderServiceItemContent);
                omcOrderServiceItemContent.setOrderServiceItemId(omcOrderServiceItem.getId());
                omcOrderServiceItemContents.add(omcOrderServiceItemContent);
            }
        }
        int addOrderServiceItemContents = omcOrderServiceItemContentService.batchSave(omcOrderServiceItemContents);
        if(addOrderServiceItemRet >= 0 && addOrderServiceItemContents >= 0){
            return 1;
        }else{
            return 0;
        }

    }

    private void checkHasSave(OrderServiceItemDto orderServiceItemDto) {
        OmcServiceItem serviceItem = omcServiceItemService.selectByKey(orderServiceItemDto.getServiceItemId());

        //检验是否上传拍照
        if(serviceItem.getItemType().equals(0)){
            if(orderServiceItemDto.getUrl() == null || orderServiceItemDto.getUrl().equals("")){
                throw new OmcBizException(ErrorCodeEnum.OMC10033006);
            }
        }

        //检验是否上传异常报告
        if(serviceItem.getItemType().equals(1)){
            if(orderServiceItemDto.getOrderServiceItemContentDtos() == null || orderServiceItemDto.getOrderServiceItemContentDtos().size() < 1){
                throw new OmcBizException(ErrorCodeEnum.OMC10033007);
            }
            List<OrderServiceItemContentDto> orderServiceItemContentDtos = orderServiceItemDto.getOrderServiceItemContentDtos();
            int flag = 0;
            for(OrderServiceItemContentDto dto : orderServiceItemContentDtos){
                if(dto.getContent() != null && !dto.getContent().equals("")){
                    flag++;
                }
            }
            if(flag<1){
                throw new OmcBizException(ErrorCodeEnum.OMC10033007);
            }
        }

        //校验服务项是否重复添加
        Example example = new Example(OmcOrderServiceItem.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("orderId" , orderServiceItemDto.getOrderId());
        criteria.andEqualTo("serviceItemId",orderServiceItemDto.getServiceItemId());
        int ret = selectCountByExample(example);
        if(ret > 0){
            throw new OmcBizException(ErrorCodeEnum.OMC10033005);
        }
    }

    private void checkOrderExist(Long orderId) {
        OmcOrder order = omcOrderService.selectByKey(orderId);
        if(order == null){
            throw new OmcBizException(ErrorCodeEnum.OMC10032008);
        }else{
            if(!order.getStat().equals(5)){
                throw new OmcBizException(ErrorCodeEnum.OMC10032008);
            }
        }
    }

    /*
    * 通过订单id查找订单服务项
    * */
    @Override
    public List<OrderServiceItemVo> selectByOrderId(Long orderId) {
        List<OrderServiceItemVo> orderServiceItemVos = omcOrderServiceItemMapper.selectByOrderId(orderId);
        return orderServiceItemVos;
    }

}
